package cn.sdhuijin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sdhuijin.model.Target;
import cn.sdhuijin.service.DeductionService;
import cn.sdhuijin.service.TargetService;
import cn.sdhuijin.utils.CommonUtil;

/**
 * @author sunp@sdhuijin.cn:
 * @version 创建时间：2015年5月4日 下午4:20:45 类说明
 */

@Controller
public class ColumnChartController {

	@Inject
	private DeductionService deductionService;

	@Inject
	private TargetService targetService;

	@RequestMapping("/getPie")
	@ResponseBody
	public Map<String, Object> getPie(@RequestParam("id") Long id) {
		Target curTarget = targetService.isExist(id);
		if (curTarget != null) {
			Map<String, Object> result = new HashMap<String, Object>();
			Map<String, Object> nativateData = deductionService
					.countStandardFolderPercent(id);
			Double[] percent = (Double[]) nativateData.get("percent");
			String[] sName = (String[]) nativateData.get("sName");

			List<Map<String, Object>> pieData = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < sName.length; i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("name", sName[i]);
				map.put("value", percent[i]);
				map.put("color", CommonUtil.getRandColorCode());
				pieData.add(map);
			}
			result.put("data", pieData);
			result.put("target", curTarget.getName());
			return result;
		} else {
			return null;
		}

	}

	@RequestMapping("/getColumnData")
	@ResponseBody
	public Map<String, Object> getColumnData() {

		Map<String, Object> nativateData = deductionService
				.countAllDetachmentSumScore();

		Map<String, Object> map = new HashMap<String, Object>();
		Long[] xId = (Long[]) nativateData.get("xId");

		Double[] frontMonth = (Double[]) nativateData.get("frontMonth");
		Double[] currentMonth = (Double[]) nativateData.get("currentMonth");
		String[] xName = (String[]) nativateData.get("xName");
		List<Double> fMonth = new ArrayList<Double>();
		List<Double> cMonth = new ArrayList<Double>();
		List<String> name = new ArrayList<String>();
		List<Long> id = new ArrayList<Long>();
		for (int i = 0; i < frontMonth.length; i++) {
			fMonth.add(frontMonth[i]);
		}
		for (int j = 0; j < currentMonth.length; j++) {
			cMonth.add(currentMonth[j]);
		}
		for (int k = 0; k < xName.length; k++) {
			name.add(xName[k]);
		}
		for (int l = 0; l < xId.length; l++) {
			id.add(xId[l]);
		}
		map.put("fMonth", fMonth);
		map.put("cMonth", cMonth);
		map.put("name", name);
		map.put("id", id);

		return map;
	}

}
